package org.example.tomandjerry;

import java.io.PrintStream;
import java.io.UnsupportedEncodingException;

public class TomAndJerry {
    public static void main(String[] args) throws UnsupportedEncodingException {
        PrintStream ps = new PrintStream(System.out, true, "UTF-8");
        Cat c = new Cat("tom");//实例化tom对象
        Mouse m = new Mouse("jerry");//实例化jerry对象
        Dog d = new Dog("Speike");//实例化speike对象
        Tool t1 = new Tool("平底锅", "吃饭", 300, "铁材质");//实例化工具1
        Tool t2 = new Tool("棒球棒", "打棒球", 900, "金属材质");//实例化工具2
        Tool t3 = new Tool("水管", "装修", 1500, "钢材质");//实例化工具3
        //调用方法
        //tom用工具t1打jerry不小心误打了speike
        c.use(t1, ps, m, d);
        //jerry用工具t2打tom
        m.use(t2, ps, c);
        //speike用工具t3打tom
        d.use(t3, ps, c);
        //tom被揍几次
        ps.println(c.getName() + "被揍" + c.count + "次");
        //speike被揍几次
        ps.println(d.getName() + "被揍" + d.count + "次");
        //jerry被揍几次
        ps.println(m.getName() + "被揍" + m.count + "次");
    }
}

abstract class Animal {
    public String name;//动物的名称

    public Integer count = 0;//表示被打的次数

    public abstract void use(Tool tool, PrintStream ps, Animal ...animal);//动物揍动物的行为

    public String print(Tool tool, Animal ...animal) {
        String p = this.name + "拿" + tool.getAction() + "用的" + tool.getWeight() + "g的" + tool.getMaterial() + "的" + tool.getTool() + "揍" + animal[0].name;
        return animal.length == 1 ? p : (p += "，不小心揍成了" + animal[animal.length-1].name);
    }
}

class Cat extends Animal {
    public Cat(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void use(Tool tool, PrintStream ps, Animal ...animal) {
        ps.println(print(tool, animal));
        tool.hit(animal[animal.length - 1]);
    }
}

class Dog extends Animal {
    public Dog(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void use(Tool tool, PrintStream ps, Animal... animal) {
        ps.println(print(tool, animal));
        tool.hit(animal[animal.length - 1]);
    }
}

class Mouse extends Animal {
    public Mouse(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void use(Tool tool, PrintStream ps, Animal ...animal) {
        ps.println(print(tool, animal));
        tool.hit(animal[animal.length - 1]);
    }
}

class Tool {
    private String tool;
    private String action;
    private Integer weight;
    private String material;

    public Tool(String tool, String action, Integer weight, String material) {
        this.tool = tool;
        this.action = action;
        this.weight = weight;
        this.material = material;
    }

    public String getAction() {
        return action;
    }

    public Integer getWeight() {
        return weight;
    }

    public String getMaterial() {
        return material;
    }

    public String getTool() {
        return tool;
    }

    public void hit(Animal animal) {
        animal.count ++;
    }
}

